package org.example.mouse;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

import com.sun.jna.*;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.ptr.IntByReference;

import java.util.Arrays;
import java.util.List;

public class RazerMouseController {

    public interface ChromaLib extends Library {
        ChromaLib INSTANCE = Native.load("RzChromaConnectAPI64", ChromaLib.class);

        // 初始化SDK
        int Init();
        int Uninit();

        // 鼠标相关函数
        int CreateMouseEffect(int effect, Pointer param, IntByReference effectId);
        int SetEffect(int effectId);
        int DeleteEffect(int effectId);
    }

    // Chroma SDK常量定义
    public static final int RZLED_ALL = 0x0100;
    public static final int CHROMA_NONE = 0;
    public static final int CHROMA_CUSTOM = 1;
    public static final int CHROMA_CUSTOM2 = 2;

    // 鼠标LED布局 - Razer鼠标通常有7x9的LED网格
    public static final int MOUSE_LED_ROWS = 9;
    public static final int MOUSE_LED_COLS = 7;

    public static class ChromaColor extends Structure {
        public byte red;
        public byte green;
        public byte blue;

        public ChromaColor(int r, int g, int b) {
            red = (byte)r;
            green = (byte)g;
            blue = (byte)b;
        }

        protected List<String> getFieldOrder() {
            return Arrays.asList("red", "green", "blue");
        }
    }

    public static class MouseCustomEffect extends Structure {
        public ChromaColor[][] color = new ChromaColor[MOUSE_LED_ROWS][MOUSE_LED_COLS];

        public MouseCustomEffect() {
            // 初始化所有LED为黑色(关闭)
            for (int r = 0; r < MOUSE_LED_ROWS; r++) {
                for (int c = 0; c < MOUSE_LED_COLS; c++) {
                    color[r][c] = new ChromaColor(0, 0, 0);
                }
            }
        }

        protected List<String> getFieldOrder() {
            return Arrays.asList("color");
        }
    }

    public static void moveMouse(int x, int y) {
        ChromaLib chroma = ChromaLib.INSTANCE;
        IntByReference effectId = new IntByReference();

        // 初始化SDK
        if (chroma.Init() != 0) {
            System.err.println("Failed to initialize Chroma SDK");
            return;
        }

        // 创建自定义效果
        MouseCustomEffect effect = new MouseCustomEffect();

        // 计算LED位置 (简化版)
        int ledX = Math.min(Math.max(x, 0), MOUSE_LED_COLS-1);
        int ledY = Math.min(Math.max(y, 0), MOUSE_LED_ROWS-1);

        // 设置"光标"位置为白色
        effect.color[ledY][ledX] = new ChromaColor(255, 255, 255);

        // 创建效果
        if (chroma.CreateMouseEffect(CHROMA_CUSTOM2, effect.getPointer(), effectId) == 0) {
            // 应用效果
            chroma.SetEffect(effectId.getValue());

            // 保持效果一段时间
            try { Thread.sleep(100); } catch (InterruptedException e) {}

            // 清除效果
            chroma.DeleteEffect(effectId.getValue());
        }

        // 卸载SDK
        chroma.Uninit();
    }

    public static void main(String[] args) {
        RazerMouseController controller = new RazerMouseController();

        // 获取当前鼠标位置
//        WinDef.POINT currentPos = MouseController.getMousePosition();
//        System.out.printf("当前鼠标位置: (%d, %d)\n", currentPos.x, currentPos.y);

        // 移动鼠标到屏幕中心
        int centerX = User32.INSTANCE.GetSystemMetrics(WinUser.SM_CXSCREEN) / 2;
        int centerY = User32.INSTANCE.GetSystemMetrics(WinUser.SM_CYSCREEN) / 2;

        smoothMove(centerX, centerY, centerX + 100, centerY + 100, 500);
        // 模拟鼠标从左上到右下移动
//        for (int i = 0; i < 7; i++) {
//            controller.moveMouse(i, i);
//            try { Thread.sleep(200); } catch (InterruptedException e) {}
//        }
    }


    public static void smoothMove(int startX,int startY,int targetX, int targetY, int durationMs) {
        int steps = 10;

        for (int i = 1; i <= steps; i++) {
            float t = (float)i / steps;
            int x = (int)(startX + (targetX - startX) * t);
            int y = (int)(startY + (targetY - startY) * t);
            moveMouse(x, y);
            try { Thread.sleep(durationMs/steps); } catch (InterruptedException e) {}
        }
    }
}
